﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AgileTimeTracker.Client.Wpf.Converters
{
    public static class GlobalConverter
    {
        public static double StringToDouble(string str)
        {            
            var number = 0.0;
            double.TryParse(str, NumberStyles.Number, CultureInfo.CurrentUICulture, out number);
            return number;
        }

        public static string DoubleToString(double value, int? decimalPlaces = null)
        {
            var number = (double)value;
            if (decimalPlaces.HasValue)
            {
                number = Math.Round(number, decimalPlaces.Value);
            }

            return number.ToString(CultureInfo.CurrentUICulture);
        }

        public static object DoubleOrString(object value, Type targetType, object parameter = null)
        {
            if (targetType == typeof(string))
            {
                if (parameter != null)
                {
                    int decimalPlaces = 0;
                    int.TryParse(parameter.ToString(), out decimalPlaces);
                    return DoubleToString((double)value, decimalPlaces);
                }

                return DoubleToString((double)value);
            }

            return StringToDouble(value as string);
        }        

        public static bool VisibilityToBool(Visibility value)
        {
            return value == Visibility.Visible;
        }

        public static Visibility BoolToVisibility(bool value)
        {
            return value ? Visibility.Visible : Visibility.Collapsed;
        }

        public static object BoolOrVisibility(object value, Type targetType)
        {
            if (targetType == typeof(Visibility))
            {
                return BoolToVisibility((bool)value);
            }

            return VisibilityToBool((Visibility)value);
        }

        public static string SecondsToTimeString(double seconds)
        {
            var time = TimeSpan.FromSeconds(seconds);
            return string.Format("{0:00}:{1:00}:{2:00}", time.Hours, time.Minutes, time.Seconds);
        }

        public static double TimeStringToSeconds(string timeStr)
        {
            if (string.IsNullOrWhiteSpace(timeStr))
            {
                return 0;
            }

            var timeValues = timeStr.Split(':').Select(t => int.Parse(t)).ToArray();
            var time = new TimeSpan(timeValues[0], timeValues[1], timeValues[2]);
            return time.TotalSeconds;
        }

        public static object SecondsOrTimeString(object value, Type targetType)
        {
            if (targetType == typeof(string))
            {
                return SecondsToTimeString((double)value);
            }

            return TimeStringToSeconds(value as string);
        }
    }
}
